<!-- import sqlite from '../../common/sqlite_test.js' -->
<template>
	<view class="half-page">
		<view class="title">原始接口0</view>
		<view style="display: flex;flex-direction: row;flex-wrap: wrap;justify-content:flex-start;justify-items: auto;">
			<button style="width: 30%;margin-bottom: 10rpx;" @click="openDB">打开DB</button>
			<button style="width: 30%;margin-bottom: 10rpx;" @click="isopen">是否打开</button>
			<button style="width: 30%;margin-bottom: 10rpx;" @click="closeDB">关闭DB</button>
			<button style="width: 30%;margin-bottom: 10rpx;" @click="executeSQL">执行SQL</button>
			<button style="width: 30%;margin-bottom: 10rpx;" @click="selectSQL">查询SQL</button>
			<button style="width: 30%;margin-bottom: 10rpx;" @click="transBegin">事务开始</button>
			<button style="width: 30%;margin-bottom: 10rpx;" @click="editDB">岁数+1</button>
			<button style="width: 30%;margin-bottom: 10rpx;" @click="transCommit">事务提交</button>
			<button style="width: 30%;margin-bottom: 10rpx;" @click="transRollback">事务回滚</button>
		</view>

	</view>
</template>

<script>
	import sqlite from '../../utils/sqlite_test.js'
	export default {
		data() {
			return {}
		},
		methods: {
			async openDB() {
				let res = await sqlite.openDB()
				if (res.code == 1) uni.$showMsg(res.msg);
				else uni.$showMsg(res.msg);
			},
			async isopen() {
				let res = await sqlite.isopen()
				if (res.code == 1) uni.$showMsg(res.msg);
				else uni.$showMsg(res.msg);
			},
			async closeDB() {
				let res = await sqlite.closeDB()
				if (res.code == 1) uni.$showMsg(res.msg);
				else uni.$showMsg(res.msg);
			},
			async executeSQL() {
				// //创建表
				// let table = 'create table if not exists database("where" CHAR(110),"location" CHAR(100),"age" INT(11))'
				// let res = await sqlite.executeSQL(table)
				// if (res.code === 1) {
				// 	//创建数据
				// 	let sql = "insert into database values('北京 ','安乐林 ','11 ')"
				// 	let res = await sqlite.executeSQL(sql)
				// 	if (res.code == 1) {
				// 		uni.$showMsg('数据插入成功')
				// 	} else {
				// 		uni.$showMsg('数据插入失败')
				// 	}
				// } else {
				// 	uni.$showMsg('表创建成功');
				// }
				let sqltxt = [
					'create table if not exists database("where" CHAR(110),"location" CHAR(100),"age" INT(11))',
					"insert into database values('河北','神州','34')"
				];
				let res = await sqlite.executeSQL(sqltxt);
				if (res.code === 1) return uni.$showMsg(res.msg)
				else uni.$showMsg(res.msg)
			},
			async selectSQL() {
				let sqlSQL = 'select * from database'
				let res = await sqlite.selectSQL(sqlSQL)
				if (res.code == 1) {
					for (var i in res.data) {
						uni.$showMsg('查询sql数据: 第' + i + '条数据' + JSON.stringify(res.data[i]))
					}
				} else {
					uni.$showMsg(res.msg)
				}
			},
			async transBegin() {
				let res = await sqlite.transaction('begin')
				if (res.code === 1) {
					uni.$showMsg(res.msg);
				} else {
					uni.$showMsg(res.msg);
				}
			},
			async transCommit() {
				let res = await sqlite.transaction('commit')
				if (res.code == 1) uni.$showMsg(res.msg);
				else uni.$showMsg(res.msg);
			},
			async transRollback() {
				let res = await sqlite.transaction('rollback')
				if (res.code == 1) uni.$showMsg(res.msg);
				else uni.$showMsg(res.msg);
			},
			async editDB() {
				let sqlSQL = 'update database set age=age+1'
				let res = await sqlite.executeSQL(sqlSQL)
				// if (res.code == 1) uni.$showMsg('修改数据成功');
				// else uni.$showMsg('修改数据失败');
			}
		}
	}
</script>

<style lang="scss">
	.half-page {
		width: 50vw;
		/* 视口宽度的50% */
		height: auto;
		/* 高度自适应 */
		position: absolute;
		/* 绝对定位 */
		top: 0;
		/* 顶部对齐 */
		left: 25vw;
		/* 左边距为视口宽度的25%，即右边屏幕一半的位置 */
		background-color: #fff;
		/* 背景色 */
		box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
		/* 阴影效果 */
	}
</style>